package com.m4f.web.controller;

import java.util.logging.Logger;

import javax.cache.CacheException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import com.m4f.business.model.Company;
import com.m4f.business.model.Participant;
import com.m4f.business.service.AppConfigurationService;
import com.m4f.business.service.CompanyService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import com.m4f.mail.Mailer;
import com.m4f.utils.StackTraceUtil;

@Controller
@RequestMapping(value="/participant")
public class ParticipantController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ParticipantController.class.getName());
	
	@Autowired
	private AppConfigurationService appConfigurationService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private Mailer mailer;
	
	public ParticipantController() throws CacheException {
		super();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String getCreateForm(Model model) {
		model.addAttribute("participant", new Participant());
		return "participant/register";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Participant participant, 
			BindingResult result, Model model) {
		if (result.hasErrors()) {
			return "participant/register";
		}
		if(this.companyService.existCompany(participant)) {
			FieldError objError = new FieldError("participant","email", 
					participant.getEmail(), false, null, null, 
					this.getMessage("register.action.user.alreadyexist", null)); 
			result.addError(objError);
			return "participant/register";
		}
		try {
			this.companyService.addCompany(participant);
		} catch(Exception e) {
			LOGGER.severe("Create participant FAIL: " + 
					StackTraceUtil.getStackTrace(e));
		}
		String msgBody = this.getMessage("user.register.welcome.mail.body", 
				participant.getEmail(), participant.getPassword()); 
		try {
			mailer.sendEmail(this.appConfigurationService.getGlobalConfiguration().getAdminMail(), 
				participant.getEmail(), this.getMessage("user.register.welcome.mail.subject", 
				null),msgBody);
		} catch(Exception e) {
			LOGGER.severe("Error sending mail " + 
					StackTraceUtil.getStackTrace(e));
		}
		String message = this.getMessage("user.register.process.ok", null);
		model.addAttribute("message", message);
		return "common/thanks";
	}

	@RequestMapping(value="/remember", method=RequestMethod.GET)
	public @ResponseBody String sendCredentials(@RequestParam String email) {
		Company company = this.companyService.
			getCompanyByParticipant(email);
		if(company == null) {
			return this.getMessage("remember.action.mail.fail", email);
		}
		Participant participant = company.getParticipantInfo();
		if(participant == null) {
			return this.getMessage("remember.action.mail.fail", email);
		}
		String msgBody = this.getMessage("user.register.data.remember.mail.body", 
				participant.getEmail(), participant.getPassword()); 
		try {
			mailer.sendEmail(this.appConfigurationService.getGlobalConfiguration().getAdminMail(), 
				participant.getEmail(), this.getMessage("user.register.data.remember.mail.subject", 
				null), msgBody);
		} catch(Exception e) {
			LOGGER.severe("Error sending mail " + 
					StackTraceUtil.getStackTrace(e));
		}
		return this.getMessage("remember.action.sendmail.ok", email);
	}
	
}